package com.lagou.rpc.consumer.task;

import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.util.ZkUtil;
import org.apache.curator.framework.CuratorFramework;

import java.util.Map;

public class ReportToZkTask implements Runnable {

    @Override
    public void run() {

        ZkUtil zkUtil = new ZkUtil();

        // 节点信息<节点，节点内容>
        Map<String, String> addressList = zkUtil.getAddressList();

        for (Map.Entry<String, String> nodeEntry : addressList.entrySet())
        {
            // 节点内容 = 请求时间：响应时间
            String value = nodeEntry.getValue();

            System.out.println("节点：" + nodeEntry.getKey() + "的内容为：" + value);

            // 节点最后一次请求时间
            long lastReqTime = Long.valueOf(value.split(":", -1)[0]);
            // 当前时间
            long currentTime = System.currentTimeMillis();
            if (currentTime - lastReqTime > 5000)
            {
                System.out.println("节点：" + nodeEntry.getKey() + "的最后一次请求时间超过5秒，响应时间置0，节点内容为："
                        + System.currentTimeMillis() + ":" + 0);
                try {
                    zkUtil.updateNodeData(nodeEntry.getKey(), System.currentTimeMillis() + ":" + 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
